<?php

class OwnerTable extends Doctrine_Table {
	private static function value($token) {
		return hash(sfConfig::get('app_crypto_hash'), substr($token, 0, 12));
	}

	public function findOrCreateOwnerByYubi($token) {
		$value = self::value($token);
		$owner = $this->findOneBy('value', $value);
		if(!$owner) {
			$log = sfContext::getInstance()->getLogger();
			$owner = new Owner();
			$owner->value = $value;
		}
		$owner->setToken($token);
		return $owner;
	}

	public function revokeOneByCode($code) {
		$owner = $this->findOneBy('revocation', hash(sfConfig::get('app_crypto_hash'), $code));
		if($owner) {
			$owner->delete();
			return true;
		}
		return false;
	}
}
